import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Dialogs 1.1
import QtQuick.Controls 1.4
import QtQuick.Controls.Styles 1.4
import wsn.func 1.1

Window {
    id: main_windows
    visible: true
    width: 1920
    height: 1080
    title: qsTr("图片分类")

    property int screen_width: 1920
    property int screen_height: 1080
    property double screen_zoom: 1
    property string input_folder_name: ""
    property string input_folder_split_name: ""
    property string output_folder_name: ""
    property string current_file_name: ""
    property int current_file_loaction: 0
    property bool status_run: false

    Component {
        id: button_style
        ButtonStyle {
            background: Rectangle {
                implicitWidth: 120
                implicitHeight: 35
                border.width: control.activeFocus ? 2 : 1
                border.color: "#888"
                radius: 4
                gradient: Gradient {
                    GradientStop { position: 0 ; color: control.pressed ? "#ccc" : "#eee" }
                    GradientStop { position: 1 ; color: control.pressed ? "#aaa" : "#ccc" }
                }
            }
        }
    }
    Component {
        id:type_style
        ButtonStyle {
                background: Rectangle {
                    implicitWidth: 90
                    implicitHeight: 35
                    border.width: control.activeFocus ? 2 : 1
                    border.color: "#888"
                    radius: 4
                    gradient: Gradient {
                        GradientStop { position: 0 ; color: control.pressed ? "#ccc" : "#eee" }
                        GradientStop { position: 1 ; color: control.pressed ? "#aaa" : "#ccc" }
                    }
                }
            }
    }

    Component.onCompleted: {
        //console.log(Screen.desktopAvailableWidth);
        //console.log(Screen.desktopAvailableHeight);
        if(screen_width !== Screen.desktopAvailableWidth){
            screen_width = Screen.desktopAvailableWidth;
            main_windows.width = screen_width;
            console.log("resize screen width to " + screen_width);
        }
        if(screen_height !== Screen.desktopAvailableHeight){
            screen_height = Screen.desktopAvailableHeight;
            main_windows.height = screen_height;
            console.log("resize screen height to " + screen_height);
        }
        screen_zoom = screen_width/1920.0;
        //console.log(screen_zoom);
        rects.resize(screen_zoom);
    }

    Rects{
        id:rects;
        x:20
        y:20
        ///
    }
    
    Func{
        id:func
    }
    
    Label {
        text: "文件位置："
        x: screen_width * 0.7
        y: screen_height * 0.05
        color: "black"
        font.family: "WenQuanYi Zen Hei"
        font.pointSize: 18
        font.bold: true
    }

    Label {
        id: input_folder
        text: ""
        x: screen_width * 0.8
        y: screen_height * 0.05
        width:screen_width * 0.2
        wrapMode:Text.Wrap
        color: "black"
        font.family: "WenQuanYi Zen Hei"
        font.pointSize: 14
        font.bold: true
    }

    Label {
        text: "输出位置："
        x: screen_width * 0.7
        y: screen_height * 0.15
        color: "black"
        font.family: "WenQuanYi Zen Hei"
        font.pointSize: 18
        font.bold: true
    }

    Label {
        id: output_folder
        text: ""
        x: screen_width * 0.8
        y: screen_height * 0.15
        width:screen_width * 0.2
        wrapMode:Text.Wrap
        color: "black"
        font.family: "WenQuanYi Zen Hei"
        font.pointSize: 14
        font.bold: true
    }

    Button{
        x:screen_width * 0.90
        y:screen_height * 0.1
        id:open_file_in;
        style: button_style
        text: "选择文件夹"
        onClicked: {
            fileDialog_in.open();
        }
    }
    Button{
        x:screen_width * 0.90
        y:screen_height * 0.2
        id:open_file_out;
        style: button_style
        text: "选择文件夹"
        onClicked: {
            fileDialog_out.open();
        }
    }

    
    Button{
        x:screen_width * 0.80
        y:screen_height * 0.28
        id:load;
        style: type_style
        text: qsTr("加载")
        onClicked: {
            if(input_folder_name === "" || output_folder_name === ""){
                empty.open()
            }
            else{
                var names = func.load();
                current_file_loaction = 0;
                status_run = true;//
                set_button_name(names);
                input_folder_split_name = input_folder_name + '-split/P';
                current_file_name = func.before()
                rects.set_picture(input_folder_split_name,current_file_name);
                rects.set_location(0);
            }
        }
    }

    Button{
        x:screen_width * 0.72
        y:screen_height * 0.28
        id:save;
        style: type_style
        text: qsTr("保存")
        onClicked: {
            func.save()
        }
    }

    Button{
        x:screen_width * 0.25
        y:screen_height * 0.80
        id:last;
        style: type_style
        text: qsTr("上一张")
        onClicked: {
            last_picture();
        }
    }

    Button{
        x:screen_width * 0.35
        y:screen_height * 0.80
        id:next;
        style: type_style
        text: qsTr("下一张")
        onClicked: {
            next_picture();
        }
        //visible: false
    }

    Button{
        x:screen_width * 0.75
        y:screen_height * 0.35
        id:c0
        //objectName:c0
        style: type_style
        text: qsTr("新类型")
        onClicked: {
            if(c0.text !== "新类型"){
                select(c0.text);
            }
        }
    }

    Button{
        x:screen_width * 0.85
        y:screen_height * 0.35
        id:c1
        //objectName:c1
        style: type_style
        text: qsTr("新类型")
        onClicked: {
            if(c1.text !== "新类型"){
                select(c1.text);
            }
        }
    }
    Button{
        x:screen_width * 0.75
        y:screen_height * 0.41
        id:c2
        //objectName:c2
        style:type_style
        text: qsTr("新类型")
        onClicked: {
            if(c2.text !== "新类型"){
                select(c2.text);
            }
        }
    }
    Button{
        x:screen_width * 0.85
        y:screen_height * 0.41
        id:c3
        //objectName:c3
        style: type_style
        text: qsTr("新类型")
        onClicked: {
            if(c3.text !== "新类型"){
                select(c3.text);
            }
        }
    }

    Button{
        x:screen_width * 0.75
        y:screen_height * 0.47
        id:c4
        //objectName:c4
        style: type_style
        text: qsTr("新类型")
        onClicked: {
            if(c4.text !== "新类型"){
                select(c4.text);
            }
        }
    }

    Button{
        x:screen_width * 0.85
        y:screen_height * 0.47
        id:c5
        //objectName:c5
        style: type_style
        text: qsTr("新类型")
        onClicked: {
            if(c5.text !== "新类型"){
                select(c5.text);
            }
        }
    }

    Button{
        x:screen_width * 0.75
        y:screen_height * 0.53
        id:c6
        //objectName:c6
        style: type_style
        text: qsTr("新类型")
        onClicked: {
            if(c6.text !== "新类型"){
                select(c6.text);
            }
        }
    }

    Button{
        x:screen_width * 0.85
        y:screen_height * 0.53
        id:c7
        //objectName:c7
        style: type_style
        text: qsTr("新类型")
        onClicked: {
            if(c7.text !== "新类型"){
                select(c7.text);
            }
        }
    }

    Button{
        x:screen_width * 0.75
        y:screen_height * 0.59
        id:c8
        //objectName:c8
        style: type_style
        text: qsTr("新类型")
        onClicked: {
            if(c8.text !== "新类型"){
                select(c8.text);
            }
        }
    }

    Button{
        x:screen_width * 0.85
        y:screen_height * 0.59
        id:c9
        //objectName:c9
        style: type_style
        text: qsTr("新类型")
        onClicked: {
            if(c9.text !== "新类型"){
                select(c9.text);
            }
        }
    }

    Button{
        x:screen_width * 0.75
        y:screen_height * 0.65
        id:c10
        //objectName:c10
        style: type_style
        text: qsTr("新类型")
        onClicked: {
            if(c10.text !== "新类型"){
                select(c10.text);
            }
        }
    }

    Button{
        x:screen_width * 0.85
        y:screen_height * 0.65
        id:c11
        //objectName:c11
        style: type_style
        text: qsTr("新类型")
        onClicked: {
            if(c11.text !== "新类型"){
                select(c11.text);
            }
        }
    }

    Button{
        x:screen_width * 0.75
        y:screen_height * 0.71
        id:c12
        //objectName:c12
        style: type_style
        text: qsTr("新类型")
        onClicked: {
            if(c12.text !== "新类型"){
                select(c12.text);
            }
        }
    }

    Button{
        x:screen_width * 0.85
        y:screen_height * 0.71
        id:c13
        //objectName:c13
        style: type_style
        text: qsTr("新类型")
        onClicked: {
            if(c13.text !== "新类型"){
                select(c13.text);
            }
        }
    }
    
    FileDialog{
        id:fileDialog_in
        title: "Please choose a folder"
        //nameFilters: ["All Files (*.*)"]
        selectFolder: true
        onAccepted: {
            var filepath = new String(fileUrl);
            if(Qt.platform.os === "windows"){
                input_folder_name = filepath.slice(8);
            }else{
                input_folder_name = filepath.slice(7);
            }
            input_folder.text = input_folder_name;
            func.list_file(input_folder_name);
        }
        onRejected: {

        }
    }

    FileDialog{
        id:fileDialog_out
        title: "Please choose a folder"
        //nameFilters: ["All Files (*.*)"]
        selectFolder: true
        onAccepted: {
            var filepath = new String(fileUrl);
            if(Qt.platform.os === "windows"){
                output_folder_name = filepath.slice(8);
            }else{
                output_folder_name = filepath.slice(7);
            }
            output_folder.text = output_folder_name;
            func.list_folder(output_folder_name);
        }
        onRejected: {

        }
    }

    MessageDialog {
        id: empty
        title: "注意"
        text: "输入或输出文件夹为空"
        onAccepted: {
        }
    }

    MessageDialog {
        id: end
        title: "注意"
        text: "最后一张图片"
        onAccepted: {
        }
    }

    function last_picture(){
        current_file_loaction -= 1;
        if(current_file_loaction < 0){
            current_file_loaction = 11;
            current_file_name = func.before()
            rects.set_picture(input_folder_split_name, current_file_name);
            rects.set_location(11);
            rects.set_label_empty();
        }else{
            rects.set_location(current_file_loaction);
        }
    }

    function set_button_name(names){
        var name_list = names.split(':');
        for(var i=0;i<name_list.length;i++){
            var t;
            switch(i){
                case 0:c0.text = name_list[i];break;
                case 1:c1.text = name_list[i];break;
                case 2:c2.text = name_list[i];break;
                case 3:c3.text = name_list[i];break;
                case 4:c4.text = name_list[i];break;
                case 5:c5.text = name_list[i];break;
                case 6:c6.text = name_list[i];break;
                case 7:c7.text = name_list[i];break;
                case 8:c8.text = name_list[i];break;
                case 9:c9.text = name_list[i];break;
                case 10:c10.text = name_list[i];break;
                case 11:c11.text = name_list[i];break;
                case 12:c12.text = name_list[i];break;
                case 13:c13.text = name_list[i];break;
            }
        }
    }

    function next_picture(){
        current_file_loaction += 1;
        if(current_file_loaction>=12){
            current_file_loaction = 0;
            current_file_name = func.next();
            rects.set_picture(input_folder_split_name, current_file_name);
            rects.set_location(0);
            rects.set_label_empty()
        }else{
            rects.set_location(current_file_loaction);
        }
    }

    function select(name){
        func.output(current_file_name + ',' + current_file_loaction.toString(), name);
        rects.set_label(current_file_loaction, name);
        next_picture();
    }
}
